<template>
  <div class="auth-container" :class="{ 'signup-mode': !isLogin }">
    <div class="form-container">
      <!-- 登录表单 -->
      <transition name="fade-slide" mode="out-in">
        <form v-if="isLogin" @submit.prevent="handleSubmit" class="auth-form">
          <h2>提货系统</h2>
          <div class="form-group">
            <input
                type="text"
                v-model="loginForm.username"
                required
                @focus="resetValidation"
            />
            <label>用户名 / 手机号</label>
          </div>
          <div class="form-group">
            <input
                type="password"
                v-model="loginForm.password"
                required
            />
            <label>密码</label>
          </div>
          <button type="submit" class="auth-btn">
            <span>立即登录</span>
            <div class="arrow">→</div>
          </button>
          <p class="toggle-text">
            新用户？
            <a @click="toggleAuth">创建账号</a>
          </p>
        </form>

        <!-- 注册表单 -->
        <form v-else @submit.prevent="handleSubmit" class="auth-form">
          <h2>加入我们</h2>
          <div class="form-group">
            <input
                type="text"
                v-model="regForm.username"
                required
            />
            <label>设置用户名</label>
            <span v-if="errors.username" class="error">{{ errors.username }}</span>
          </div>
          <div class="form-group">
            <input
                type="password"
                v-model="regForm.password"
                required
            />
            <label>设置密码</label>
          </div>
          <div class="form-group">
            <input
                type="password"
                v-model="regForm.confirmPassword"
                required
            />
            <label>确认密码</label>
            <span v-if="errors.confirm" class="error">{{ errors.confirm }}</span>
          </div>
          <button type="submit" class="auth-btn">
            <span>立即注册</span>
            <div class="arrow">→</div>
          </button>
          <p class="toggle-text">
            已有账号？
            <a @click="toggleAuth">返回登录</a>
          </p>
        </form>
      </transition>
    </div>

    <!-- 装饰图形 -->
    <div class="decoration">
      <div class="shape1"></div>
      <div class="shape2"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'

const isLogin = ref(true)
const errors = reactive({
  username: '',
  confirm: ''
})

const loginForm = reactive({
  username: '',
  password: ''
})

const regForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

// 表单验证
const validateForm = () => {
  if (!isLogin.value) {
    if (regForm.username.length < 4) {
      errors.username = '用户名至少4个字符'
      return false
    }
    if (regForm.password !== regForm.confirmPassword) {
      errors.confirm = '两次密码不一致'
      return false
    }
  }
  return true
}

// 提交处理
const handleSubmit = () => {
  if (!validateForm()) return
  console.log(isLogin.value ? '登录提交' : '注册提交')
}

// 切换表单
const toggleAuth = () => {
  isLogin.value = !isLogin.value
  resetValidation()
}

// 重置错误提示
const resetValidation = () => {
  errors.username = ''
  errors.confirm = ''
}
</script>

<style scoped>
:root {
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --background: #f8fafc;
  --text: #1e293b;
}

* {
  box-sizing: border-box;
}

.auth-container {
  display: flex;
  min-height: 100vh;
  background: var(--background);
  position: relative;
  overflow: hidden;
}

.form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  z-index: 2;
}

.auth-form {
  width: 100%;
  max-width: 400px;
  padding: 2.5rem;
  background: white;
  border-radius: 1.5rem;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  transition: transform 0.4s ease;
}

h2 {
  color: var(--text);
  font-size: 1.75rem;
  margin-bottom: 2rem;
  font-weight: 600;
}

.form-group {
  position: relative;
  margin-bottom: 1.5rem;
}

input {
  width: 100%;
  height: 50px;
  padding: 0 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 0.75rem;
  font-size: 1rem;
  transition: all 0.3s ease;
}

input:focus {
  border-color: var(--primary);
  outline: none;
}

input:focus + label,
input:not(:placeholder-shown) + label {
  transform: translateY(-130%);
  font-size: 0.875rem;
  color: var(--primary);
}

label {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  color: #64748b;
  pointer-events: none;
  transition: all 0.3s ease;
  background: white;
  padding: 0 0.25rem;
}

.auth-btn {
  width: 100%;
  height: 50px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 0.75rem;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  margin-top: 1.5rem;
  transition: all 0.3s ease;
}

.auth-btn:hover {
  background: var(--primary-hover);
}

.arrow {
  font-size: 1.25rem;
  transform: translateX(0);
  transition: transform 0.3s ease;
}

.auth-btn:hover .arrow {
  transform: translateX(4px);
}

.toggle-text {
  text-align: center;
  margin-top: 1.5rem;
  color: #64748b;
}

.toggle-text a {
  color: var(--primary);
  cursor: pointer;
  font-weight: 500;
}

.toggle-text a:hover {
  text-decoration: underline;
}

.error {
  color: #ef4444;
  font-size: 0.875rem;
  margin-top: 0.25rem;
  display: block;
}

/* 装饰图形 */
.decoration {
  position: absolute;
  width: 50vw;
  height: 100%;
  right: 0;
  top: 0;
  background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  clip-path: polygon(25% 0%, 100% 0%, 100% 100%, 0% 100%);
  transition: all 0.4s ease;
}

.shape1, .shape2 {
  position: absolute;
  background: rgba(255,255,255,0.1);
  border-radius: 50%;
}

.shape1 {
  width: 300px;
  height: 300px;
  right: -150px;
  top: -150px;
}

.shape2 {
  width: 200px;
  height: 200px;
  right: 100px;
  bottom: -100px;
}

/* 动效 */
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: opacity 0.3s, transform 0.4s;
}

.fade-slide-enter-from,
.fade-slide-leave-to {
  opacity: 0;
  transform: translateY(20px);
}

/* 响应式 */
@media (max-width: 768px) {
  .decoration {
    display: none;
  }

  .auth-form {
    padding: 2rem;
    box-shadow: none;
    background: transparent;
  }

  input {
    height: 48px;
  }
}
</style>